//app.js
var config = require('config.js');
var server = require('utils/server.js');
App({
    onLaunch: function () {
        // wx.clearStorage()
        //调用API从本地缓存中获取数据
        console.log('app onLaunch')
        var logs = wx.getStorageSync('logs') || []
        logs.unshift(Date.now())
        wx.setStorageSync('logs', logs)
        this.login();
    },
    onShow: function () {

    },
    onHide: function () {

    },

    getUserInfo: function (cb) {
        var that = this;
        if (that.globalData.openid) {
            typeof cb == "function" && cb(that.globalData.openid);
            return;
        } else {
            //调用登录接口
            wx.login({
                success: function (e) {
                    console.log('---code---', e);
                    var params = {
                        code: e.code,
                    };
                    server.getData('wxapp/index/login', params, function (res) {
                        console.log('------uwxapp/index/login-----');
                        console.log(res);
                        that.globalData.openid = res.data.userinfo.openid;
                        typeof cb == "function" && cb(that.globalData.openid)
                    })
                }
            })
        }
    },

    globalData: {
        hasLogin: false,
        openid:''
    },

    login: function () {
        var that = this;
        // 先获取微信信息
        var userInfo = wx.getStorageSync('userInfo');
        if (!userInfo) {
            wx.getUserInfo({
                success: function (res) {
                    console.log('----------------wxgetUserInfo successd-------------');
                    console.log(res);
                    wx.setStorageSync('userInfo', res.userInfo);
                }
            })
        }

        // 调用登录接口
        wx.login({
            success: function (e) {
                wx.request({
                    url: 'https://2016.bookgo.com.cn/wxapp/index/login',
                    data: {
                        code: e.code,
                    },
                    header: {
                        'content-type': 'application/json'
                    },
                    success: function (res) {
                        console.log('-----获取openid +++++ ----------->userinfo-------');
                        console.log(res.data.userinfo.openid);
                        wx.setStorageSync('openid', res.data.userinfo.openid);
                        var userInfo = wx.getStorageSync('userInfo');
                        if (!userInfo) {
                            wx.getUserInfo({
                                success: function (res) {
                                    console.log('----------------wxgetUserInfo successd-------------');
                                    console.log(res);
                                    wx.setStorageSync('userInfo', res.userInfo);
                                

                                }
                            })
                        } else {
                            
                        }


                    },
                    fail: function (res) {
                        // console.log(res)
                    }
                })
            }
        })
    }

});




